/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EmprestimoGUI.java
 *
 * Created on 16/05/2011, 03:39:18
 */

package emprestimo;

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
/**
 *
 * @author Vanessa
 */
public class EmprestimoGUI extends javax.swing.JFrame {

    /** Creates new form EmprestimoGUI */
    public EmprestimoGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Cálculo de Empréstimo");

        jLabel2.setText("Idade");

        jLabel3.setText("Sexo");

        jLabel4.setText("Salário");

        jButton1.setText("Calcular");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(jButton1)
                .addContainerGap(78, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))))))
                .addGap(47, 47, 47))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        boolean passou = false;
        if ((jTextField1.getText().equals("")) || (jTextField2.getText().equals("")) || (jTextField3.getText().equals("")))
            JOptionPane.showMessageDialog(rootPane, "Informe os dados para o valor máximo do empréstimo ser calculado", "Mensagem", JOptionPane.PLAIN_MESSAGE);
        else{
            Emprestimo emp = new Emprestimo();
            DecimalFormat df = new DecimalFormat("0.#");
            
             try{
                 emp.checaIdade(Integer.parseInt(jTextField1.getText()));
                 passou = true;
             }catch (Exception e){
                  JOptionPane.showMessageDialog(rootPane, emp.ERRO_IDADE, "Idade", JOptionPane.PLAIN_MESSAGE);
             }
            emp.setIdade(Integer.parseInt(jTextField1.getText()));
            if (passou == true){
                passou = false;
                try{
                    emp.checaSexo(jTextField2.getText().toUpperCase());
                    passou = true;
                }catch (Exception e){
                     JOptionPane.showMessageDialog(rootPane, emp.ERRO_SEXO, "Sexo", JOptionPane.PLAIN_MESSAGE);
                 }
                if (jTextField2.getText().toUpperCase().equals("F"))
                  emp.setSexo("FEMININO");
                 else if (jTextField2.getText().toUpperCase().equals("M"))
                    emp.setSexo("MASCULINO");
                else
                    emp.setSexo(jTextField2.getText().toUpperCase());
            }
            if (passou ==true){
                passou=false;
                try{
                     emp.checaSalario(Double.parseDouble(jTextField3.getText()));
                     passou=true;
                }catch (Exception e){
                    JOptionPane.showMessageDialog(rootPane, emp.ERRO_SALARIO, "Salário", JOptionPane.PLAIN_MESSAGE);
                }
                emp.setSalario(Double.parseDouble(jTextField3.getText()));
            }
            if (passou ==true){
                try{
                    emp.checaConcessao();
                    if (emp.getSexo().equals("FEMININO"))
                        JOptionPane.showMessageDialog(rootPane, "Mulher: R$"+Double.parseDouble(df.format(emp.calculaEmprestimo_M())), "Valor máximo do empréstimo", JOptionPane.PLAIN_MESSAGE);
                        
                    else
                        JOptionPane.showMessageDialog(rootPane, "Homem: R$"+Double.parseDouble(df.format(emp.calculaEmprestimo_H())), "Valor máximo do empréstimo", JOptionPane.PLAIN_MESSAGE);
                }catch (Exception e){
                    if (emp.getSexo().equals("FEMININO"))
                       JOptionPane.showMessageDialog(rootPane, emp.ERRO_CONCESSAO_M, "Erro na concessão", JOptionPane.PLAIN_MESSAGE);
                    else
                       JOptionPane.showMessageDialog(rootPane, emp.ERRO_CONCESSAO_H, "Erro na concessão", JOptionPane.PLAIN_MESSAGE);
                }
            }
    }//GEN-LAST:event_jButton1ActionPerformed
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EmprestimoGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables

}
